package com.sys.function;

import com.alibaba.fastjson2.JSON;
import com.sys.common.vo.Result;
import com.sys.group.entity.Edge;
import com.sys.group.entity.Node;
import com.sys.group.entity.Tree;
import com.sys.group.entity.User;
import com.sys.group.service.IEdgeService;
import com.sys.group.service.INodeService;
import com.sys.group.service.ITreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/save")
public class SaveTree {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ITreeService treeService;

    @Autowired
    private IEdgeService edgeService;

    @Autowired
    private INodeService nodeService;

    @PostMapping("/tree")
    public Result<Map<String, Object>> savetree(@RequestBody String treejson) {
        //System.out.println("前端传回树的json数据: " + treejson);
        //前端传回的json数据转换为STree类
        STree stree = JSON.parseObject(treejson, STree.class);
        //System.out.println("转换后的STree类数据: " + stree);
        //得到token
        String token=stree.getToken();
        //redis中查找token得到对应用户信息
        Object obj = redisTemplate.opsForValue().get(token);
        // redis中存的数据是经过序列化处理的，取出来读取前需要进行反序列化
        if (obj != null) {
            // 调用fastjson2中方法，将传入字符串反序列化为所需目前类型
            User loginUser = JSON.parseObject(JSON.toJSONString(obj), User.class);

            //创建树信息
            Tree tree=new Tree();
            tree.setTname(stree.getTreename());
            tree.setUid(loginUser.getId());
            tree.setTime(new Date());
            tree.setStatus(0);

            //将树信息写入数据库,在TreeMapper中写入数据库后将tid赋值给tree.tid
            treeService.addtree(tree);

            //递归读取树的所有节点，将边和点的信息存入数据库
            rw_STree(stree,tree.getTid());

            //System.out.println("插入数据库的树信息: " + tree);
            //测试方法，从数据库读取该用户创建的所有树
//            List<Tree> alltree=treeService.getTreesByUId(loginUser.getId());
//            for (Tree all : alltree) {
//                System.out.println("该用户创建的树信息: " + all);
//            }

            return Result.success( "保存成功");
        }else {
            return Result.fail( "保存失败");
        }
    }

    //递归方法，根据STree的内容，将其中的节点和边信息全部写入数据库
    void rw_STree(STree stree,Integer tid){
        Edge edge = new Edge();
        Node node = new Node();

        //该节点有父节点，即非根节点,写入边信息后插入数据库
        if (stree.getPid()!=null){
            edge.setTid(tid);
            edge.setParent(stree.getPid());
            edge.setChild(stree.getId());
            edgeService.addedge(edge);
        }

        //插入点信息进入数据库
        node.setId(stree.getId());
        node.setType(stree.getType());
        node.setName(stree.getName());
        node.setPercent(stree.getWeight());
        node.setValue(stree.getVal());
        node.setOperator(stree.getParam());
        node.setTid(tid);

        //插入数据库
        nodeService.addnode(node);

        System.out.println("edge:"+edge);
        System.out.println("node:"+node);
        //遍历其子节点
        if (stree.getChildren() != null) {
            for (STree child : stree.getChildren()) {
                rw_STree(child,tid); // 递归处理子节点
            }
        }
    }
}
